﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Reflection;
using HF.Software.ClassLib;
using HF.Software.Entities;

namespace HF.Software
{
    /// <summary>
    /// hef创建于2015.02.03
    /// 票务配置文件读写
    /// </summary>
    public partial class frmTic : Form
    {
        #region event

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (null == this.oConfig) return;

            IniModelDataCollection oDataCollection = new IniModelDataCollection();
            IniWrite oIniWrite = new IniWrite();

            TIC oTic = this.oConfig.TIC;

            try
            {
                PropertyInfo[] oFields = oTic.GetType().GetProperties();

                foreach (PropertyInfo oInfo in oFields)
                {
                    IniModel oConfigIni = new IniModel();
                    oConfigIni.IN_Section = "TIC";
                    oConfigIni.IN_Key = oInfo.Name;
                    oConfigIni.IN_Value = Convert.ToString(oInfo.GetValue(oTic, null));

                    switch (oConfigIni.IN_Key.Trim())
                    {
                        case "PrinterName"://打印端口
                            oConfigIni.IN_Value = this.cmbPort.SelectedValue.ToString().Trim();
                            break;

                        case "PrinterStyle"://票机型号
                            oConfigIni.IN_Value = this.cmbModel.SelectedValue.ToString().Trim();
                            break;

                        case "SleepTime"://打印延时
                            oConfigIni.IN_Value = this.txtPrintDelay.Text.Trim();
                            break;

                        case "BookPassWord"://订票初始密码
                            oConfigIni.IN_Value = this.txtInitialPwd.Text.Trim();
                            break;

                        case "PrintCinema"://打印影院名称
                            if (this.chkPrintName.Checked)
                                oConfigIni.IN_Value = "T";
                            else
                                oConfigIni.IN_Value = "F";
                            break;
                    }

                    oDataCollection.Add(oConfigIni);
                }

                foreach (IniModel oIniModel in oDataCollection)
                {
                    oIniWrite.Write(oIniModel.IN_Section, oIniModel.IN_Key, oIniModel.IN_Value);
                }
                MessageBox.Show("票务更新成功！", "提示");
            }
            catch (Exception ex)
            {
                this.oLog.fnLog("201502091456票务更新失败：" + ex.ToString());
                MessageBox.Show("票务更新失败！", "提示");
            }
        }

        #endregion

        #region fun

        /// <summary>
        /// 初始化下拉框数据
        /// </summary>
        private void fnDataBind()
        {
            #region 打印端口

            List<ClsComboBoxItem> lstCmbPort = new List<ClsComboBoxItem>();

            ClsComboBoxItem oCmbPort1 = new ClsComboBoxItem();
            oCmbPort1.Value = "LPT1";
            oCmbPort1.Text = "LPT1";
            lstCmbPort.Add(oCmbPort1);

            ClsComboBoxItem oCmbPort2 = new ClsComboBoxItem();
            oCmbPort2.Value = "LPT2";
            oCmbPort2.Text = "LPT2";
            lstCmbPort.Add(oCmbPort2);

            ClsComboBoxItem oCmbPort3 = new ClsComboBoxItem();
            oCmbPort3.Value = "LPT3";
            oCmbPort3.Text = "LPT3";
            lstCmbPort.Add(oCmbPort3);

            ClsComboBoxItem oCmbPort4 = new ClsComboBoxItem();
            oCmbPort4.Value = "COM1";
            oCmbPort4.Text = "COM1";
            lstCmbPort.Add(oCmbPort4);

            ClsComboBoxItem oCmbPort5 = new ClsComboBoxItem();
            oCmbPort5.Value = "COM2";
            oCmbPort5.Text = "COM2";
            lstCmbPort.Add(oCmbPort5);

            ClsComboBoxItem oCmbPort6 = new ClsComboBoxItem();
            oCmbPort6.Value = "COM3";
            oCmbPort6.Text = "COM3";
            lstCmbPort.Add(oCmbPort6);

            ClsComboBoxItem oCmbPort7 = new ClsComboBoxItem();
            oCmbPort7.Value = "COM4";
            oCmbPort7.Text = "COM4";
            lstCmbPort.Add(oCmbPort7);

            ClsComboBoxItem oCmbPort8 = new ClsComboBoxItem();
            oCmbPort8.Value = "USB";
            oCmbPort8.Text = "USB";
            lstCmbPort.Add(oCmbPort8);

            this.cmbPort.DataSource = lstCmbPort;
            this.cmbPort.DisplayMember = "Text";
            this.cmbPort.ValueMember = "Value";

            this.cmbPort.SelectedValue = this.oConfig.TIC.PrinterName;

            #endregion

            #region 票机型号

            List<ClsComboBoxItem> lstCmbModel = new List<ClsComboBoxItem>();

            ClsComboBoxItem oCmbModel1 = new ClsComboBoxItem();
            oCmbModel1.Value = "0";
            oCmbModel1.Text = "0.TM88";
            lstCmbModel.Add(oCmbModel1);

            ClsComboBoxItem oCmbModel2 = new ClsComboBoxItem();
            oCmbModel2.Value = "1";
            oCmbModel2.Text = "1.EP500";
            lstCmbModel.Add(oCmbModel2);

            ClsComboBoxItem oCmbModel3 = new ClsComboBoxItem();
            oCmbModel3.Value = "2";
            oCmbModel3.Text = "2.MB500";
            lstCmbModel.Add(oCmbModel3);

            ClsComboBoxItem oCmbModel4 = new ClsComboBoxItem();
            oCmbModel4.Value = "3";
            oCmbModel4.Text = "3.TEC419";
            lstCmbModel.Add(oCmbModel4);

            ClsComboBoxItem oCmbModel5 = new ClsComboBoxItem();
            oCmbModel5.Value = "4";
            oCmbModel5.Text = "4.TEC431";
            lstCmbModel.Add(oCmbModel5);

            ClsComboBoxItem oCmbModel6 = new ClsComboBoxItem();
            oCmbModel6.Value = "5";
            oCmbModel6.Text = "5.TEC4PT";
            lstCmbModel.Add(oCmbModel6);

            ClsComboBoxItem oCmbModel7 = new ClsComboBoxItem();
            oCmbModel7.Value = "6";
            oCmbModel7.Text = "6.BY6200";
            lstCmbModel.Add(oCmbModel7);

            ClsComboBoxItem oCmbModel8 = new ClsComboBoxItem();
            oCmbModel8.Value = "7";
            oCmbModel8.Text = "7.BY2002";
            lstCmbModel.Add(oCmbModel8);

            this.cmbModel.DataSource = lstCmbModel;
            this.cmbModel.DisplayMember = "Text";
            this.cmbModel.ValueMember = "Value";

            this.cmbModel.SelectedValue = this.oConfig.TIC.PrinterStyle;

            #endregion
        }

        #endregion

        #region create

        public frmTic()
        {
            InitializeComponent();
        }

        private void frmTic_Load(object sender, EventArgs e)
        {
            if (null == this.oConfig) return;

            this.fnDataBind();

            this.txtPrintDelay.Text = this.oConfig.TIC.SleepTime.ToString().Trim();

            this.txtInitialPwd.Text = this.oConfig.TIC.BookPassWord.Trim();

            this.chkPrintName.Checked = this.oConfig.TIC.PrintCinema;
        }

        #endregion

        #region var

        AppConfig oConfig = new AppConfig();
        private ClsLog oLog = new ClsLog(Application.StartupPath + @"\log\set\" + DateTime.Now.ToString("yyyyMMddHH") + ".txt");

        #endregion

    }
}
